<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

$shopId = $_GET['shopId'];
$serviceId = $_GET['serviceId'];
$startHour = $_GET['startHour'];
$startMinute = $_GET['startMinute'];
$orderId = $_GET['id'];

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['start_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$getTimeArray = explode(" ", $getDateArray[2]);
$day = $getTimeArray[0] * 1;
$getTimeArray = explode(":", $getTimeArray[1]);
$startHour = $getTimeArray[0];
$startMinute = $getTimeArray[1];


$year = $year < 1 ? $tmp_year : $year;
$month = $month < 1 ? $tmp_mon : $month;
$day = $day < 1 ? $tmp_day : $day;

$shop = Shop::getById($shopId);

$service = Service::GetById($serviceId);
$endMinute = $startMinute + $service['service_duration'];
$endHour = $startHour;
while ($endMinute > 59){
	$endMinute = $endMinute - 60;
	$endHour = $endHour + 1;
}

$tempStart = mktime($startHour, $startMinute, 0, $month, $day, $year);
$tempEnd = mktime($endHour, $endMinute, 0, $month, $day, $year);

$shopOpenTime = mktime($shop['open_hour'], $shop['open_minute'], 0, $month, $day, $year);
$shopEndTime = mktime($shop['close_hour'], $shop['close_minute'], 0, $month, $day, $year);
//Logs::setLog(" startHour=$startHour || startMinute=$startMinute || endHour=$endHour || endMinute=$endMinute"."||".$shop['open_hour']."||".$shop['close_hour']."||".$shop['open_minute']."||".$shop['close_minute']);
$canOrderIntarval = 10 * 60;
$now = time() + 0;
if( $tempStart < $now ){
	Logs::setLog("(calc time)-2||$year < $tmp_year || $month < $tmp_mon || $day < $tmp_day || $startHour < $tmp_Hour || ($startHour == $tmp_Hour && $startMinute < $tmp_Minute)");
	Output::Json("有効な予約時間ではありません、ご確認してください！", -1);
	exit;
}

if( $tempStart < $shopOpenTime || $tempEnd > $shopEndTime - $service['reset_device_time'] * 60){
 	Logs::setLog("(calc time)3||endMinute||".date("y-m-d H:i", $tempStart)."||".date("y-m-d H:i", $tempEnd)."||".date("y-m-d H:i", $shopOpenTime)."||".date("y-m-d H:i", ($shopEndTime - $service['reset_device_time'] * 60) * 1) );
	Output::Json("大変申し訳ございませんでした。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。", -1);
	exit;
}

if( ShopOrderLimit::CanBook($shopId, $service, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute + $service['reset_device_time']) != 1){
	Logs::setLog("(calc time)4");
	$temp = Preengage::GetById($orderId);
	
	if( $temp == null || $tempStart < $temp['service_start_time'] || $tempEnd > $temp['service_end_time']){
		Output::Json("大変申し訳ございませんでした。お客様ご予約したい時間帯は込み合っている為に予約取れませんでした。", -1);
		exit;
	}
};

$result = array('endHour' => $endHour, 'endMinute' => $endMinute);
Output::Json($result);
